<?php

declare(strict_types=1);

namespace App\Controller\Api;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use App\Middleware\UserMiddleware;
use App\Model\Cart as ModelCart;
use App\Model\Good;

/**
 * @AutoController(prefix="/api/cart")
 */
class Cart extends AbstractController
{
    /**
     * @Middlewares({
     *     @Middleware(UserMiddleware::class)
     * })
     */
    public function list() {
        $data = $this->request->all();
        $query = ModelCart::query();

        $query = $query->with('good');
        $query = $query->where('user_id', $this->request->getHeader('uuid')[0]);

        $query = $query->orderBy('id', 'desc');

        if (isset($data['page_size']) && $data['page_size'] <= 0) {
            $list = $query->get();
        }else{
            $list = $query->paginate($data['page_size'] ?? 20);
        }
        return $this->success($list);
    }

    /**
     * @Middlewares({
     *     @Middleware(UserMiddleware::class)
     * })
     */
    public function save() {
        $this->validate([
            'good_id' => 'required',
            'amount' => 'required|integer|gt:0',
        ]);
        $params = $this->request->all();
        $good = Good::where('id', $params['good_id'])->where('status', 1)->first();
        if ($good == null) {
            return $this->error('商品不存在');
        }

        $uuid = $this->request->getHeader('uuid')[0];
        $cart = ModelCart::query()->where('good_id', $params['good_id'])
            ->where('user_id', $uuid)
            ->first();
        
        if ($cart == null) {
            $cart = new ModelCart();
            $cart->good_id = $params['good_id'];
            $cart->user_id = $uuid;
        }
        $cart->title = $good->title;

        $cart->amount = $params['amount'];
        $cart->save();

        return $this->success($cart);
    }

    /**
     * @Middlewares({
     *     @Middleware(UserMiddleware::class)
     * })
     */
    public function deleteOne() {
        $this->validate([
            'good_id' => 'required',
        ]);
        $params = $this->request->all();

        $uuid = $this->request->getHeader('uuid')[0];
        $cart = ModelCart::query()->where('good_id', $params['good_id'])
            ->where('user_id', $uuid)
            ->first();
        if ($cart == null) {
            return $this->error();
        }

        $cart->delete();

        return $this->success();
    }

    /**
     * @Middlewares({
     *     @Middleware(UserMiddleware::class)
     * })
     */
    public function deleteMany() {
        $this->validate([
            'good_ids' => 'required|array',
        ]);
        $params = $this->request->all();

        $uuid = $this->request->getHeader('uuid')[0];
        $cart = ModelCart::query()->whereIn('good_id', $params['good_ids'])
            ->where('user_id', $uuid)
            ->delete();

        return $this->success();
    }

    /**
     * @Middlewares({
     *     @Middleware(UserMiddleware::class)
     * })
     */
    public function clearAll() {

        $uuid = $this->request->getHeader('uuid')[0];
        $cart = ModelCart::query()
            ->where('user_id', $uuid)
            ->delete();

        return $this->success();
    }
}
